home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / comms / other / novia / src / novia_global.c < prev    next >
Text File  |  1999-12-06  |  13KB  |  569 lines

  1. #include <time.h>
  2. #include <devices/timer.h>
  3. #include <pragma/timer_lib.h>
  4. #include <clib/timer_protos.h>
  5. #include <exec/types.h>
  6. #include <exec/io.h>
  7. #include <exec/memory.h>
  8. #include <devices/conunit.h>
  9. #include <intuition/intuition.h>
  10. #include <dos/dos.h>
  11. #include <libraries/fifo.h>
  12. #include <pragma/exec_lib.h>
  13. #include <pragma/graphics_lib.h>
  14. #include <pragma/dos_lib.h>
  15. #include <pragma/diskfont_lib.h>
  16. #include <pragma/intuition_lib.h>
  17. #include <pragma/gadtools_lib.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <graphics/text.h>
  22. #include <utility/date.h>
  23. #include <novia/novia_global.h>
  24. #include <novia/novia_misc.h>
  25. #include <novia/novia_funcs.h>
  26. #include <novia/novia_gui.h>
  27. #include <novia/novia_registers.h>
  28. #include <pragma/noviasys_lib.h>
  29.  
  30.  
  31.  
  32. struct TextAttr textattr[]={"cnet21",8,0,FPF_DISKFONT};
  33.  
  34. extern struct Task *MainTask;
  35. struct Library *noviasysbase;
  36.  
  37.  
  38. extern struct MainPortConfig *mainportconfig;
  39.  
  40. void GetArg(char *string);
  41. void FreeAllMem();
  42. void ClearMemQuick(APTR start,ULONG len);
  43. void ItemToViewItem(APTR itemaddress,ViewItem *viewitem);
  44. BOOL ArgToLong(const char *string,ULONG *from,ULONG *to);
  45. BOOL DelItem(const char *path,ULONG start,ULONG end);
  46. BOOL AddItem(struct ItemHeader *itemheader,const char *path);
  47. void NewShell();
  48. void CloseShell();
  49. ULONG StrToIP(char *string);
  50. void IPToStr(char *string,ULONG IPNumber);
  51. void ClearList(struct List *list);
  52. BOOL ChangeWhere(char *where);
  53.  
  54. void SelectFromTo(UBYTE *table, ULONG tablesize, char *string);
  55. extern char StdCommand(ULONG cmdid);
  56.  
  57. extern void SendOnlineMessage(PortData *cport);
  58. //extern struct Item *GetItem_byIP(ULONG dirIP,ULONG itemIP,struct Item *item,ULONG size);
  59.  
  60. extern int GetINETMail(void);
  61.  
  62. extern List *client_list;
  63. extern struct GUIFrontend guifrontend;
  64. extern struct Task *GUITask;
  65.  
  66. void GetArg(char *string)
  67. {
  68.     struct PortData *cport=(PortData *)FindTask(NULL)->tc_UserData;
  69.     if (cport)
  70.     {
  71.         UBYTE len = strlen(string);
  72.         UBYTE counter1;
  73.         UBYTE counter2;
  74.         char quit = FALSE;
  75.         char *ptr;
  76.         char *strptr;
  77.         cport->argcounter = 0;
  78.         for (counter1 = 0; counter1 < MAXARGS; counter1++)    // first, clearing old arg's
  79.         {
  80.             for (counter2 = 0; counter2 < MAXARGLEN; counter2++)
  81.                 cport->argstring[counter1][counter2] = 0;
  82.         }
  83.  
  84.         /* next part. counter1 is the offset in string.
  85.                        counter2 is the offset in cport->argstring. */
  86.  
  87.         strptr = string;
  88.         while (!quit && cport->argcounter < MAXARGLEN)
  89.         {
  90.             while (isspace(*strptr))
  91.                 *strptr++;
  92.             if (*strptr)
  93.             {
  94.                 ptr = cport->argstring[cport->argcounter];
  95.                 if (*strptr == '"')
  96.                 {
  97.                     *strptr++;
  98.                     while (*strptr && *strptr != '"')
  99.                         *ptr++ = *strptr++;
  100.                     if (*strptr == '"')
  101.                         strptr++;
  102.                 }
  103.                 else
  104.                 {
  105.                     while (*strptr && isgraph(*strptr) && *strptr != '"')
  106.                         *ptr++ = *strptr++;
  107.                 }
  108.                 *ptr = 0;
  109.                 cport->argcounter++;
  110.             }
  111.             else
  112.                 quit = TRUE;
  113.             
  114.         }
  115.         cport->argptr = cport->inputbuffer;
  116.         while (*cport->argptr && *cport->argptr!=' ')
  117.             cport->argptr++;
  118.         while (*cport->argptr && *cport->argptr==' ')
  119.             cport->argptr++;
  120.     }
  121. }
  122.  
  123.  
  124. void FreeAllMem()
  125. {
  126. /*    struct PortData *cport=(PortData *)FindTask(NULL)->tc_UserData;
  127.     if (cport)
  128.     {
  129.         MyMemEntry *entry;
  130.         APTR address;
  131.         if (alloc_mem_list)
  132.         {
  133.             entry=(MyMemEntry *)alloc_mem_list->lh_Head;
  134.             while (!IsListEmpty(alloc_mem_list))
  135.             {
  136.                 Remove((Node *)entry);
  137.                 address=entry->address;
  138.                 FreeVec(address);
  139.                 FreeVec(entry);
  140.                 entry=(MyMemEntry *)alloc_mem_list->lh_Head;
  141.             }
  142.             FreeVec(alloc_mem_list);
  143.         }
  144.     }*/
  145. }
  146.  
  147. void ClearMemQuick(APTR start,ULONG len)
  148. {
  149.     ULONG *start2=(ULONG *)start;
  150.     ULONG *end=start2+(len>>2);
  151.     while (start2<end)
  152.         *start2++=0;
  153. }
  154.  
  155. void ClearMem(APTR start,ULONG len)
  156. {
  157.     UBYTE *start2=(UBYTE *)start;
  158.     UBYTE *end=start2+len;
  159.     while (start2<end)
  160.         *start2++ = 0;
  161. }
  162.  
  163. void ItemToViewItem(APTR itemaddress,ViewItem *viewitem)
  164. {
  165.     struct ItemHeader *itemheader=itemaddress;
  166.     struct Item *item=itemaddress;
  167.     struct Subboard *subboard=itemaddress;
  168.     if (itemheader->ItemType==ITEMTYPE_SUBBOARD)
  169.     {
  170.         if (!strlen(subboard->Scanfilter))strcpy(subboard->Scanfilter,"dir");
  171.         sprintf(viewitem->Title," c3%4d       c3%s                                                    ",viewitem->ListID+1,itemheader->Title);
  172.         sprintf(&viewitem->Title[61],"c7(c3%3sc7)  -  c1[%02d-%02d-%02d - %02d:%02d:%02d]",subboard->Scanfilter,
  173.             itemheader->LastChange.day,
  174.             itemheader->LastChange.month,
  175.             itemheader->LastChange.year,
  176.             itemheader->LastChange.hours,
  177.             itemheader->LastChange.mins,
  178.             itemheader->LastChange.secs);
  179.     }
  180.     else
  181.     {
  182.         sprintf(viewitem->Title," c3%4d       c7%s                                                     ",viewitem->ListID+1,itemheader->Title);
  183.         sprintf(&viewitem->Title[50],"c3%10dc7 bytes, %3d responds  -  c1[%02d-%02d-%02d - %02d:%02d:%02d]",item->Size,item->replys,
  184.                 itemheader->LastChange.day,
  185.                 itemheader->LastChange.month,
  186.                 itemheader->LastChange.year,
  187.                 itemheader->LastChange.hours,                itemheader->LastChange.mins,
  188.                 itemheader->LastChange.secs);
  189.     }
  190.     if (viewitem->select)
  191.         *viewitem->Title='*';
  192. }
  193.  
  194. BOOL ArgToLong(const char *string,ULONG *from,ULONG *to)
  195. {
  196.     struct PortData *cport=(PortData *)FindTask(NULL)->tc_UserData;
  197.     if (cport)
  198.     {
  199.         UBYTE counter;
  200.         UBYTE counter2=0;
  201.         LONG ulong;
  202.         char  string2[25];
  203.         if (!stricmp(string,"ALL"))
  204.         {
  205.             *from=0;
  206.             *to=0xFFFFFFFF;
  207.             return TRUE;
  208.         }
  209.         if (StrToLong((STRPTR)string,&ulong)==-1|strlen(string)==0)return FALSE;
  210.         for (counter=0;(counter<12 && string[counter]>='0' && string[counter]<='9');counter++)
  211.         {
  212.             string2[counter]=string[counter];
  213.         }
  214.         StrToLong(string2,(LONG *)from);
  215.         counter++;
  216.         string2[0]=0;
  217.         for (;(counter<22 && string[counter]>='0' && string[counter]<='9');counter++)
  218.         {
  219.             string2[counter2]=string[counter];
  220.             counter2++;
  221.         }
  222.         string2[counter2]=0;
  223.         if (StrToLong(string2,(LONG *)to)==-1)
  224.         {
  225.             if (string[counter-1]=='-')
  226.             {
  227.                 *to=0xFFFFFFFF;
  228.             }
  229.             else
  230.             {
  231.                 *to=*from;
  232.             }
  233.         }
  234.     }
  235.     return TRUE;
  236. }
  237.  
  238.  
  239. BOOL DelItem(const char *path,ULONG start,ULONG end)
  240. {
  241.     struct PortData *cport=(PortData *)FindTask(NULL)->tc_UserData;
  242.     if (cport)
  243.     {
  244.         BPTR index_mulder;
  245.         BPTR data_mulder;
  246.         char *buffer;
  247.         APTR pointer;
  248.         ULONG ulong;
  249.     
  250.         struct IndexHeader indexheader;
  251.     
  252.         if (buffer=AllocVec(256,MEMF_ANY))
  253.         {
  254.             strcpy(buffer,path);
  255.             strcat(buffer,"/data/.data");
  256.             if (index_mulder=Open(buffer,MODE_READWRITE))
  257.             {
  258.                 if (Read(index_mulder,&indexheader,sizeof(IndexHeader))==sizeof(IndexHeader))
  259.                 {
  260.                     if (end>indexheader.Entrys-1)end=indexheader.Entrys-1;
  261.                     if (start>end)
  262.                     {
  263.                         ulong=end;
  264.                         end=start;
  265.                         start=ulong;
  266.                     }
  267.                     strcpy(buffer,path);
  268.                     strcat(buffer,"/data/.data");
  269.                     if (data_mulder=Open(buffer,MODE_READWRITE))
  270.                     {
  271.     
  272.                         /*** End of .data-file is the offset for new subboard-structure ***/
  273.     
  274.                         Seek(data_mulder,0,OFFSET_END);
  275.                         ulong=Seek(data_mulder,0,OFFSET_END);    // get offset for .index-entry
  276.     
  277.                         if (pointer=AllocVec(ulong,MEMF_ANY))
  278.                         {    
  279.                             Seek(index_mulder,12,OFFSET_BEGINNING);
  280.                             Seek(index_mulder,sizeof(IndexEntry)*(end+1),OFFSET_CURRENT);
  281.                             if (end+1<indexheader.Entrys)
  282.                             {
  283.                                 FRead(index_mulder,pointer,(indexheader.Entrys-end-1)*sizeof(IndexEntry),1);
  284.                                 Seek(index_mulder,12,OFFSET_BEGINNING);
  285.                                 Seek(index_mulder,sizeof(IndexEntry)*start,OFFSET_CURRENT);
  286.                                 FWrite(index_mulder,pointer,(indexheader.Entrys-end-1)*sizeof(IndexEntry),1);
  287.                             }
  288.                             Seek(index_mulder,8,OFFSET_BEGINNING);
  289.                             indexheader.Entrys=indexheader.Entrys-(end-start+1);
  290.                             FWrite(index_mulder,&indexheader.Entrys,4,1);
  291.                             SetFileSize(index_mulder,12+indexheader.Entrys*sizeof(IndexEntry),OFFSET_BEGINNING);
  292.                             cport->readnew=TRUE;
  293.                             FreeVec(pointer);
  294.                         }
  295.                         Close(data_mulder);
  296.                     }
  297.                     else
  298.                     {
  299.                         ioprintf("\nCan't open %s. ",path);
  300.                         PrintDosError(IoErr());
  301.                     }
  302.                     Close(index_mulder);
  303.                 }
  304.                 else
  305.                 {
  306.                     ioprintf("\nCan't read %s. ",path);
  307.                     PrintDosError(IoErr());
  308.                 }
  309.             }
  310.             else
  311.             {
  312.                 ioprintf("\nCan't open %s. ",path);
  313.                 PrintDosError(IoErr());
  314.                 return FALSE;
  315.             }
  316.         }
  317.     }
  318. }
  319.     
  320. BOOL AddItem(struct ItemHeader *itemheader,const char *path)
  321. {
  322.     struct PortData *cport=(PortData *)FindTask(NULL)->tc_UserData;
  323.     if (cport)
  324.     {
  325.         BPTR index_mulder=NULL;
  326.         BPTR data_mulder=NULL;
  327.         BPTR lock;
  328.         BPTR lock2;
  329.         char *buffer;
  330.         APTR pointer;
  331.         ULONG ulong;
  332.         char validate=FALSE;
  333.         struct Item *item=(Item *)itemheader;
  334.         struct Subboard *subboard=(Subboard *)itemheader;
  335.         struct IndexEntry *indexentry;
  336.         struct IndexHeader indexheader;
  337.     
  338.         if (!(indexentry=AllocVec(sizeof(IndexEntry),MEMF_ANY)))
  339.         {
  340.             return FALSE;
  341.         }
  342.     
  343.         if (buffer=AllocVec(256,MEMF_ANY))
  344.         {
  345.             if (itemheader->ItemType=ITEMTYPE_SUBBOARD)
  346.             {
  347.                 if (!(lock=Lock(itemheader->Path,SHARED_LOCK)))    // First locking of exists directory
  348.                 {
  349.                     if (!(lock=CreateDir(itemheader->Path)))            // create directory.
  350.                     {
  351.                         ioprintf("\nCan't create directory. %s,",itemheader->Path);
  352.                         PrintDosError(IoErr());
  353.                     }
  354.                 }
  355.                 if (lock)
  356.                 {
  357.                     UnLock(lock);
  358.                     strcpy(buffer,itemheader->Path);
  359.                     strcat(buffer,"/data");
  360.                     if (!(lock=Lock(buffer,SHARED_LOCK)))
  361.                     {
  362.                         if (!(lock=CreateDir(buffer)))
  363.                         {
  364.                             ioprintf("\nCan't Create %s. ",buffer);
  365.                             PrintDosError(IoErr());
  366.                         }
  367.                     }
  368.                 }
  369.                 if (lock)
  370.                 {
  371.                     UnLock(lock);
  372.                     validate=TRUE;
  373.                 }
  374.             }
  375.             else
  376.             {
  377.                 validate=TRUE;
  378.             }
  379.             if (validate)
  380.             {
  381.                 strcpy(buffer,path);
  382.                 strcat(buffer,"/data/.index");
  383.                 if (index_mulder=Open(buffer,MODE_READWRITE))
  384.                 {
  385.                     strcpy(buffer,path);
  386.                     strcat(buffer,"/data/.data");
  387.                     if (data_mulder=Open(buffer,MODE_READWRITE))
  388.                     {
  389.                         if (Read(index_mulder,&indexheader,sizeof(IndexHeader))<sizeof(IndexHeader))
  390.                         {
  391.                             ClearMemQuick(&indexheader,sizeof(IndexHeader));
  392.                             Seek(index_mulder,0,OFFSET_BEGINNING);
  393.                         }
  394.     
  395.                         /*** Adding Username & ID of Creater ***/
  396.     
  397.                         item->FromUser=cport->LocalUser.IDNumber;
  398.                         item->ItemType=1;
  399.                         strcpy(item->Name,cport->LocalUser.Handle);
  400.                         strcpy(item->FromRealName,cport->LocalUser.RealName);
  401.                         strcpy(item->Filename,cport->LocalUser.Handle);
  402.                         GetTime(&itemheader->CreateDate);
  403.                         GetTime(&itemheader->LastChange);
  404.     
  405.                         /*** End of .data-file is the offset for new subboard-structure ***/
  406.     
  407.                         ClearMemQuick(indexentry,sizeof(IndexEntry));
  408.                         Seek(data_mulder,0,OFFSET_END);
  409.                         indexentry->offset=Seek(data_mulder,0,OFFSET_END);
  410.                         itemheader->IndexOffset=indexentry->offset;
  411.     
  412.                         /*** Calculating IDNumber of subboard ***/
  413.     
  414.                         itemheader->IDNumber=indexheader.idcounter++;
  415.                         indexheader.Entrys++;
  416.                         Seek(index_mulder,0,OFFSET_BEGINNING);
  417.                         Write(index_mulder,&indexheader,sizeof(IndexHeader));
  418.     
  419.                         /*** Adding subboard-Entry in .index ***/
  420.     
  421.                         indexentry->type=itemheader->ItemType;
  422.                         Seek(index_mulder,0,OFFSET_END);
  423.                         Write(index_mulder,indexentry,sizeof(IndexEntry));
  424.     
  425.                         /*** Saveing Subdir-Structure in .data and closing X-Files ***/
  426.     
  427.                         if (itemheader->ItemType==ITEMTYPE_SUBBOARD)
  428.                         {
  429.                             Write(data_mulder,itemheader,sizeof(Subboard));
  430.                         }
  431.                         else
  432.                         {
  433.                             Write(data_mulder,itemheader,sizeof(Item));
  434.                         }
  435.     
  436.                         Close(data_mulder);
  437.                     }
  438.                     else
  439.                     {
  440.                         ioprintf("\nCan't open %s. ",path);
  441.                         PrintDosError(IoErr());
  442.                         return FALSE;
  443.                     }
  444.                     Close(index_mulder);
  445.                 }
  446.                 else
  447.                 {
  448.                     ioprintf("\nCan't open %s. ",path);
  449.                     PrintDosError(IoErr());
  450.                     return FALSE;
  451.                 }
  452.             }
  453.             FreeVec(buffer);
  454.             return TRUE;
  455.         }
  456.         return FALSE;
  457.     }
  458. }
  459.     
  460. void NewShell()
  461. {
  462. }
  463.  
  464. void CloseShell()
  465. {
  466. }
  467.  
  468. ULONG StrToIP(char *string)
  469. {
  470.     BYTE counter,counter2;
  471.     ULONG IPNumber=0;
  472.     BYTE *add=(BYTE *)&IPNumber;
  473.     LONG erg;
  474.     IPNumber=0;
  475.     counter=0;
  476.     for (counter2=0;counter2<4;counter2++)
  477.     {
  478.         StrToLong(&string[counter],(long *)&erg);
  479.         add[counter2]=(BYTE)erg;
  480.         for (counter=counter;string[counter]!='.';counter++)
  481.         {
  482.             if (!string[counter])return IPNumber;
  483.         }
  484.         counter++;
  485.     }
  486.     return IPNumber;
  487. }
  488.  
  489. void IPToStr(char *string,ULONG IPNumber)
  490. {
  491.     ULONG ip=IPNumber;
  492.     UBYTE *add=(char *)&ip;
  493.     sprintf(string,"%0d.%0d.%0d.%0d",add[0],add[1],add[2],add[3]);
  494. }
  495.  
  496.  
  497. void ClearList(struct List *list)
  498. {
  499.     while (list->lh_Head->ln_Succ)
  500.         FreeVec(RemHead(list));
  501. }
  502.  
  503.  
  504. BOOL ChangeWhere(char *where)
  505. {
  506.     struct PortData *cport=(PortData *)FindTask(NULL)->tc_UserData;
  507.     if (cport)
  508.     {
  509.         strncpy(cport->Where,where,sizeof(cport->Where));
  510.         return TRUE;
  511.     }
  512.     else
  513.         return FALSE;
  514. }
  515.  
  516.  
  517. ULONG searchstr(char *str1, char *str2)
  518. {
  519.     char *pos1=str1;
  520.     char *pos2=str2;
  521.     if (*str1 && *str2)
  522.     {
  523.         while (*pos1 && *pos2)
  524.         {
  525.             if (tolower(*pos1)==tolower(*pos2))
  526.                 pos2++;
  527.             else
  528.                 pos2=str2;
  529.             pos1++;
  530.         }
  531.     }
  532.     else
  533.         return NULL;
  534.  
  535.     if (!*pos2)
  536.         return ((pos1-str1)-(pos2-str2)+1);
  537.     else
  538.         return NULL;
  539.  
  540. }
  541.  
  542. ULONG searchkeyword(char *str1, char *str2)
  543. {
  544.     char *pos1=str1;
  545.     char *pos2=str2;
  546.     if (*str1 && *str2)
  547.     {
  548.         while (*pos1 && *pos2)
  549.         {
  550.             if (tolower(*pos1)==tolower(*pos2))
  551.                 pos2++;
  552.             else
  553.                 pos2=str2;
  554.             pos1++;
  555.         }
  556.     }
  557.     else
  558.         return NULL;
  559.  
  560. //    while (*pos1==':' | *pos1=='=' | *pos1==' ')
  561.  
  562.     if (!*pos2)
  563.         return ((pos1-str1)-(pos2-str2)+1);
  564.     else
  565.         return NULL;
  566.  
  567. }
  568.  
  569.